﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Repository;

namespace RooHui.OA.Biz
{
    public class RoleModuleBiz
    {
        public static List<RoleModule> GetRoleModuleByRoleID(int roleID)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var data = (from c in ctx.Module
                                join di in ctx.RoleModule.Where(f=>f.RoleID==roleID)
                                 on  c.ID equals di.ModuleID into dt
                                from d in dt.DefaultIfEmpty()
                                select new { c, d })
                                    .ToList()
                                    .Select(e => new RoleModule().Set(e.d).LoadModule(e.c))
                                    .ToList();

                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static bool RoleAuthorizeDelete(int roleID)
        {
            bool isTrue = true;
            try
            {
                using (Context ctx = new Context())
                {
                    int count = ctx.RoleModule.Count(c => c.RoleID == roleID);
                    if (count == 0)
                    {
                        return true;
                    }
                   int row= ctx.Delete<U_RoleModule>()
                       .WhereSet(c => c.RoleID, WhereOperator.Equal, roleID)
                       .End()
                       .Excute();
                   if (row > 0)
                   {
                       isTrue = true;
                   }
                   else
                   {
                       isTrue = false;
                   }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                isTrue = false;
            }
            return isTrue;
        }

        public static bool RoleAuthorize(int roleID, string moduleIDs)
        {
            bool isTrue=true;
            try
            {
                using (Context ctx = new Context())
                { 
                    foreach (var moduleId in moduleIDs.Split(','))
                    {
                        var roleModule = new RoleModule()
                        {
                            RoleID=roleID,
                            ModuleID=Convert.ToInt32(moduleId)
                        };
                        roleModule.PostAdd();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                isTrue = false;
            }
            return isTrue;
        }

    }
}
